package com.itheima.mm.controller;

import com.itheima.framework.annotation.HmComponent;
import com.itheima.framework.annotation.HmRequestMapping;
import com.itheima.framework.annotation.HmSetter;
import com.itheima.mm.base.BaseController;
import com.itheima.mm.common.GlobalConst;
import com.itheima.mm.entity.PageResult;
import com.itheima.mm.entity.QueryPageBean;
import com.itheima.mm.entity.Result;
import com.itheima.mm.pojo.Course;
import com.itheima.mm.pojo.User;
import com.itheima.mm.service.CourseService;
import com.itheima.mm.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.rmi.ServerException;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author ：wanghao
 * @date ：Created in 2020/10/28
 * @description ：
 * @version: 1.0
 */
@HmComponent
@Slf4j
public class CourseController extends BaseController {

    @HmSetter("courseService")
    private CourseService courseService;

    /**
     * 新增学科
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServerException
     */
    @HmRequestMapping("/course/add")
    public void addCourse(HttpServletRequest request, HttpServletResponse response) throws IOException, ServerException {
        try {
            // 封装客户端数据到pojo对象
            Course course = parseJSON2Object(request, Course.class);
            log.debug("表单数据：{}", course);
            // 初始化表单数据
            course.setCreateDate(DateUtils.parseDate2String(new Date()));
            // 获取用户ID
            //HttpSession session = request.getSession(false);
            //User user = (User)session.getAttribute(GlobalConst.SESSION_KEY_USER);
            User user = getSessionUser(request, GlobalConst.SESSION_KEY_USER);
            // 为了调试，默认userId为1
            course.setUserId(user != null ? user.getId() : 1);
            log.debug("添加完初始化数据的course:{}",course);
            // 调用service完成业务
            courseService.addCourse(course);
            // 响应json
            printResult(response,new Result(true,"添加学科成功"));
        } catch (RuntimeException re) {
            log.error("", re);
            re.printStackTrace();
            printResult(response, new Result(false, "添加学科失败" + re.getMessage()));
        }
    }

    /**
     * 分页展示学科数据
     * @param request
     * @param response
     * @throws IOException
     * @throws ServerException
     */
    @HmRequestMapping("/course/findListByPage")
    public void findListByPage(HttpServletRequest request, HttpServletResponse response) throws IOException, ServerException {
        try {
            // 获取查询参数
            QueryPageBean queryPageBean = parseJSON2Object(request,QueryPageBean.class);
            log.debug("queryPageBean:{}",queryPageBean);
            if (queryPageBean == null){
                queryPageBean = new QueryPageBean();
                queryPageBean.setCurrentPage(1);
                queryPageBean.setPageSize(10);
            }
            // 调用service，获取PageResult
            //PageResult pageResult = new PageResult(13L,new ArrayList());
            PageResult pageResult = courseService.findListByPage(queryPageBean);
            // 响应json
            printResult(response,new Result(true,"查询成功",pageResult));
        }catch (RuntimeException re){
            log.error("",re);
            re.printStackTrace();
            printResult(response,new Result(false,"获取学科列表失败"+re.getMessage()));
        }
    }

    /**
     * 更新学科
     * @param request
     * @param response
     * @throws IOException
     * @throws ServerException
     */
    @HmRequestMapping("/course/updateCourse")
    public void updateCourse(HttpServletRequest request, HttpServletResponse response) throws IOException, ServerException {
        try {
            // 将前端传过来的数据转为json
            Course course = parseJSON2Object(request,Course.class);
            log.debug("前端传来的数据course:{}",course);
            courseService.updateCourse(course);
            printResult(response,new Result(true,"修改学科成功"));
        }catch (RuntimeException re){
            log.debug("",re);
            re.printStackTrace();
            printResult(response,new Result(false,"修改学科失败"));
        }
    }

    /**
     * 删除学科
     * @param request
     * @param response
     * @throws IOException
     * @throws ServerException
     */
    @HmRequestMapping("/course/deleteCourse")
    public void deleteCourse(HttpServletRequest request, HttpServletResponse response) throws IOException, ServerException {
        try {
            String courseId = request.getParameter("courseId");
            courseService.deleteCourse(Integer.parseInt(courseId));
            printResult(response,new Result(true,"删除学科成功"));
        }catch (RuntimeException re){
            log.error("",re);
            re.printStackTrace();
            printResult(response,new Result(false, re.getMessage()));
        }
    }
}
